package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;

import models.desease.*;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class DiseaseDAO {
	
	DataSource dataSource;
	JdbcTemplate jdbc;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		jdbc = new JdbcTemplate(dataSource);
	}
	
	public HashMap<String, Disease> getAllDiseases() {
		String SQL = "SELECT name, color FROM FACTORYDATA.DISEASES";
		HashMap<String, Disease> diseases = new HashMap<String, Disease>();
		List<Disease> getAllDiseases = jdbc.query(SQL, new RowMapper<Disease>() {
			@Override
			public Disease mapRow(ResultSet rs, int rowNum) throws SQLException {
				Disease disease = new MainDisease(rs.getString("name"), rs.getString("color"));
				return disease;
			}
		});
		for (Disease d : getAllDiseases)
		{
			diseases.put(d.getName(), d);
		}
		return diseases;
	}
	
}
